import Vue from 'vue'
import axios from 'axios'

var user
var Authorization
var login
var register
var isLogin = false

export function setuser (data) {
  Authorization = data.data.token
  user = data
  isLogin = true
}

export function getLoginState (data) {
  return isLogin
}

export function loginOut (data) {
  Authorization = data.data.token
  user = data
  isLogin = false
}

export function getuser () {
  return user
}

export function getuserId () {
  return user.data.user.userId
}

export function getAuthorization () {
  return Authorization
}

export function setLoginTele (func) {
  login = func
}
export function openLogin () {
  login()
}

export function setRegisterTele (func) {
  register = func
}
export function openRegister () {
  register()
}


export function autoLogin (user) {
  return axios.post('/mscs/user/login', Vue.paramsConvert(user))
}

export function setCookie (c_name,c_pwd,exdays) {
  // var exdate=new Date();//获取时间
  // exdate.setTime(exdate.getTime() + 24*60*60*1000*exdays);//保存的天数
  // //字符串拼接cookie
  // window.document.cookie="userName"+ "=" +c_name+";path=/;expires="+exdate.toGMTString();
  // window.document.cookie="userPwd"+"="+c_pwd+";path=/;expires="+exdate.toGMTString();

  //字符串拼接cookie
  window.document.cookie="userName"+ "=" +c_name+";path=/";
  window.document.cookie="userPwd"+"="+c_pwd+";path=/";
}

export function getCookie () {
  let user = {}
  if (document.cookie.length>0) {
    var arr=document.cookie.split('; ');//这里显示的格式需要切割一下自己可输出看下
    for(var i=0;i<arr.length;i++){
      var arr2=arr[i].split('=');//再次切割
      //判断查找相对应的值
      if(arr2[0]=='userName'){
        user.userName=arr2[1];//保存到保存数据的地方
      }else if(arr2[0]=='userPwd'){
        user.password=arr2[1];
      }
    }
  }
  // 取出用户名密码自动登陆
  return user
}

//清除cookie
 export function clearCookie () {
   setCookie("","",-1);//修改2值都为空，天数为负1天就好了
 }
